################################################################################
# \file defines.mk
#
# \brief
# Defines, needed for the PSOC(TM) 6 build recipe.
#
################################################################################
# \copyright
# (c) 2018-2025, Cypress Semiconductor Corporation (an Infineon company)
# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################

ifeq ($(WHICHFILE),true)
$(info Processing $(lastword $(MAKEFILE_LIST)))
endif

include $(MTB_TOOLS__RECIPE_DIR)/make/recipe/defines_common.mk


################################################################################
# General
################################################################################
_MTB_RECIPE__PROGRAM_INTERFACE_SUPPORTED:=KitProg3 JLink
_MTB_RECIPE__DEFAULT_PROGRAM_INTERFACE:=KitProg3
#
# Compactibility interface for this recipe make
#
MTB_RECIPE__INTERFACE_VERSION:=2
MTB_RECIPE__EXPORT_INTERFACES:=1 2 3 4

MTB_RECIPE__NINJA_SUPPORT:=1 2

#
# List the supported toolchains
#
ifdef CY_SUPPORTED_TOOLCHAINS
MTB_SUPPORTED_TOOLCHAINS?=$(CY_SUPPORTED_TOOLCHAINS)
else
MTB_SUPPORTED_TOOLCHAINS?=GCC_ARM IAR ARM A_Clang LLVM_ARM
endif

# For BWC with Makefiles that do anything with CY_SUPPORTED_TOOLCHAINS
CY_SUPPORTED_TOOLCHAINS:=$(MTB_SUPPORTED_TOOLCHAINS)

_MTB_RECIPE__START_FLASH:=0x10000000

ifeq ($(MTB_TYPE),PROJECT)
_MTB_RECIPE__IS_MULTI_CORE_APPLICATION:=true
endif

ifneq (,$(findstring TVIIBE,$(_MTB_RECIPE__DEVICE_DIE)))
_MTB_RECIPE__IS_TVII_DEVICE:=true
endif

#
# Core specifics
#
_MTB_RECIPE__OPENOCD_SECOND_RESET_TYPE:=run

ifeq (,$(_MTB_RECIPE__IS_MULTI_CORE_DEVICE))
ifeq ($(MTB_RECIPE__CORE),CM0P)
$(call mtb__error,$(DEVICE) does not have a CM0+ core)
endif
_MTB_RECIPE__OPENOCD_CORE:=cm4
_MTB_RECIPE__OPENOCD_EXTRA_PORT_FLAG:=
_MTB_RECIPE__OPENOCD_EXTRA_PORT_ECLIPSE:=
_MTB_RECIPE__OPENOCD_CM0_DISABLE_FLAG:=set ENABLE_CM0 0
_MTB_RECIPE__OPENOCD_CM0_DISABLE_ECLIPSE:=-c &quot;$(_MTB_RECIPE__OPENOCD_CM0_DISABLE_FLAG)&quot;&\#13;&\#10;
_MTB_RECIPE__OPENOCD_GDBINIT_FILE:=gdbinit
_MTB_RECIPE__JLINK_GDBINIT_FILE:=gdbinit_jlink
else
ifeq ($(MTB_RECIPE__CORE),CM0P)
_MTB_RECIPE__OPENOCD_CORE:=cm0
_MTB_RECIPE__OPENOCD_SECOND_RESET_TYPE:=init
_MTB_RECIPE__OPENOCD_GDBINIT_FILE:=gdbinit_cm0
_MTB_RECIPE__JLINK_GDBINIT_FILE:=gdbinit_cm0_jlink

else
_MTB_RECIPE__OPENOCD_CORE:=cm4
_MTB_RECIPE__OPENOCD_EXTRA_PORT_FLAG:=gdb_port 3332
_MTB_RECIPE__OPENOCD_EXTRA_PORT_ECLIPSE:=-c &quot;$(_MTB_RECIPE__OPENOCD_EXTRA_PORT_FLAG)&quot;&\#13;&\#10;
_MTB_RECIPE__OPENOCD_CM0_DISABLE_FLAG:=
_MTB_RECIPE__OPENOCD_CM0_DISABLE_ECLIPSE:=
_MTB_RECIPE__OPENOCD_GDBINIT_FILE:=gdbinit
_MTB_RECIPE__JLINK_GDBINIT_FILE:=gdbinit_jlink
endif
endif #(,$(_MTB_RECIPE__IS_MULTI_CORE_DEVICE))

#
# Secure targets specifics
#
ifneq (,$(_MTB_RECIPE__IS_SECURE_DEVICE))
_MTB_RECIPE__OPENOCD_GDBINIT_FILE:=gdbinit_secure
_MTB_RECIPE__OPENOCD_EXTRA_PORT_FLAG:=
_MTB_RECIPE__OPENOCD_EXTRA_PORT_ECLIPSE:=
_MTB_RECIPE__OPENOCD_SECOND_RESET_TYPE:=init
_MTB_RECIPE__OPENOCD_DO_CONTINUE=true
_MTB_RECIPE__OPENOCD_SET_STOP_AT=true
_MTB_RECIPE__OPENOCD_RUN_RESTART_CMD_DEBUG_ECLIPSE:=mon reset $(_MTB_RECIPE__OPENOCD_SECOND_RESET_TYPE)&\#13;&\#10;flushregs
ifeq ($(MTB_RECIPE__CORE),CM0P)
_MTB_RECIPE__OPENOCD_TARGET_AP:=set TARGET_AP cm0_ap
else
_MTB_RECIPE__OPENOCD_TARGET_AP:=set TARGET_AP cm4_ap
_MTB_RECIPE__OPENOCD_TARGET_AP_DEBUG:=set TARGET_AP cm4_ap
_MTB_RECIPE__CM0_CM4_ELF_FILE_APPLICATION:=$(_MTB_RECIPE__VSCODE_ELF_FILE_APPLICATION)
ifneq (,$(_MTB_RECIPE__IS_MULTI_CORE_APPLICATION))
_MTB_RECIPE__OPENOCD_TARGET_AP_DEBUG:=gdb_port 3332;set TARGET_AP cm0_cm4_ap
_MTB_RECIPE__OPENOCD_RUN_RESTART_CMD_DEBUG_ECLIPSE:=mon reset $(_MTB_RECIPE__OPENOCD_SECOND_RESET_TYPE)&\#13;&\#10;flushregs&\#13;&\#10;monitor targets psoc64.cpu.cm0&\#13;&\#10;monitor resume&\#13;&\#10;monitor targets psoc64.cpu.cm4
endif
endif # ifeq ($(MTB_RECIPE__CORE),CM0P)
_MTB_RECIPE__OPENOCD_RUN_RESTART_CMD_ATTACH_ECLIPSE:=flushregs
_MTB_RECIPE__OPENOCD_SMIF_DISABLE:=set DISABLE_SMIF 1
_MTB_RECIPE__OPENOCD_SMIF_DISABLE_ECLIPSE:=-c &quot;$(_MTB_RECIPE__OPENOCD_SMIF_DISABLE)&quot;&\#13;&\#10;
_MTB_RECIPE__OPENOCD_TARGET_AP_ECLIPSE:=-c &quot;$(_MTB_RECIPE__OPENOCD_TARGET_AP)&quot;&\#13;&\#10;
_MTB_RECIPE__OPENOCD_TARGET_AP_DEBUG_ECLIPSE:=-c &quot;$(_MTB_RECIPE__OPENOCD_TARGET_AP_DEBUG)&quot;&\#13;&\#10;
CY_OPENOCD_SHELL_TIMEOUT_CMD:=shell sleep 5
CY_OPENOCD_SHELL_TIMEOUT_ECLIPSE:=$(CY_OPENOCD_SHELL_TIMEOUT_CMD)&\#13;&\#10;
_MTB_RECIPE__MULTICORE_SECOND_CONFIG_ECLIPSE=Add CM0+ to CM4
_MTB_RECIPE__MULTICORE_SECOND_CONFIG_VSCODE=CM0+
else # ifneq (,$(_MTB_RECIPE__IS_SECURE_DEVICE))
_MTB_RECIPE__OPENOCD_RUN_RESTART_CMD_DEBUG_ECLIPSE:=mon reset $(_MTB_RECIPE__OPENOCD_SECOND_RESET_TYPE)&\#13;&\#10;mon psoc6 reset_halt sysresetreq&\#13;&\#10;flushregs&\#13;&\#10;mon gdb_sync&\#13;&\#10;thread apply all stepi&\#13;&\#10;tbreak main&\#13;&\#10;thread apply all continue
_MTB_RECIPE__OPENOCD_RUN_RESTART_CMD_ATTACH_ECLIPSE:=flushregs&\#13;&\#10;mon gdb_sync&\#13;&\#10;stepi
_MTB_RECIPE__MULTICORE_SECOND_CONFIG_ECLIPSE=Add CM4 to CM0+
_MTB_RECIPE__MULTICORE_SECOND_CONFIG_VSCODE=CM4
_MTB_RECIPE__OPENOCD_DO_CONTINUE=false
_MTB_RECIPE__OPENOCD_SET_STOP_AT=false
ifneq (,$(_MTB_RECIPE__IS_TVII_DEVICE))
_MTB_RECIPE__OPENOCD_RUN_RESTART_CMD_DEBUG_ECLIPSE:=mon reset $(_MTB_RECIPE__OPENOCD_SECOND_RESET_TYPE)&\#13;&\#10;mon traveo2 reset_halt sysresetreq&\#13;&\#10;flushregs&\#13;&\#10;mon gdb_sync&\#13;&\#10;thread apply all stepi&\#13;&\#10;tbreak main&\#13;&\#10;thread apply all continue
endif
endif

#
# Architecure specifics
#
_MTB_RECIPE__OPENOCD_MONITOR_CMDS_NAME:=psoc6
_MTB_RECIPE__MCU_NAME:=PSoC6

ifneq (,$(_MTB_RECIPE__IS_SECURE_DEVICE))

_MTB_RECIPE__OPENOCD_CHIP_NAME:=psoc64
_MTB_RECIPE__JLINKSCRIPT_ATTACH_CORE:=_CM4
_MTB_RECIPE__JLINKSCRIPT_PROGRAM_CORE:=_CM4
_MTB_RECIPE__JLINKSCRIPT_TM_POST_FIX:=_tm
_MTB_RECIPE__JLINK_DEBUG_SUFFIX:=_tm
ifeq ($(MTB_RECIPE__CORE),CM0P)
_MTB_RECIPE__JLINKSCRIPT_ATTACH_CORE:=_CM0p
endif

else ifneq (,$(_MTB_RECIPE__IS_TVII_DEVICE))

_MTB_RECIPE__MCU_NAME:=TRAVEO T2G
_MTB_RECIPE__FLASH_VAR:=$(DEVICE)
_MTB_RECIPE__NO_QSPI:=1
_MTB_RECIPE__JLINK_SECT:=
_MTB_RECIPE__JLINK_DEBUG_SUFFIX:=
_MTB_RECIPE__JLINKSCRIPT_TM_POST_FIX:=
_MTB_RECIPE__JLINKSCRIPT_PROGRAM_CORE:=_M0
_MTB_RECIPE__JLINKSCRIPT_ATTACH_CORE:=_M4
ifeq ($(MTB_RECIPE__CORE),CM0P)
_MTB_RECIPE__JLINKSCRIPT_ATTACH_CORE=_M0
endif
_MTB_RECIPE__OPENOCD_MONITOR_CMDS_NAME:=traveo2

else #(,$(_MTB_RECIPE__IS_SECURE_DEVICE))

_MTB_RECIPE__OPENOCD_CHIP_NAME:=psoc6
_MTB_RECIPE__JLINKSCRIPT_SECT128:=_sect128KB
_MTB_RECIPE__JLINKSCRIPT_SECT256:=_sect256KB
ifeq ($(MTB_RECIPE__CORE),CM0P)
_MTB_RECIPE__JLINKSCRIPT_ATTACH_CORE:=_CM0p
_MTB_RECIPE__JLINK_DEBUG_SUFFIX:=_tm
else
_MTB_RECIPE__JLINKSCRIPT_ATTACH_CORE:=_CM4
_MTB_RECIPE__JLINK_DEBUG_SUFFIX:=
endif

ifeq (,$(_MTB_RECIPE__IS_MULTI_CORE_DEVICE))
_MTB_RECIPE__JLINKSCRIPT_TM_POST_FIX:=
_MTB_RECIPE__JLINKSCRIPT_PROGRAM_CORE:=_CM4
else
_MTB_RECIPE__JLINKSCRIPT_TM_POST_FIX:=_tm
_MTB_RECIPE__JLINKSCRIPT_PROGRAM_CORE:=_CM0p
endif

ifneq (,$(_MTB_RECIPE__IS_MULTI_CORE_APPLICATION))
_MTB_RECIPE__OPENOCD_CM0_RTOS_CONFIG=-c &quot;$(_MTB_RECIPE__OPENOCD_CHIP_NAME).cpu.cm0 configure -rtos auto -rtos-wipe-on-reset-halt 1&quot;&\#13;&\#10;
_MTB_RECIPE__OPENOCD_CM4_RTOS_CONFIG=-c &quot;$(_MTB_RECIPE__OPENOCD_CHIP_NAME).cpu.cm4 configure -rtos auto -rtos-wipe-on-reset-halt 1&quot;&\#13;&\#10;
endif

endif #(,$(_MTB_RECIPE__IS_SECURE_DEVICE))

ifeq (PSoC6ABLE2,$(_MTB_RECIPE__DEVICE_DIE))
_MTB_RECIPE__OPENOCD_DEVICE_CFG=psoc6.cfg
_MTB_RECIPE__JLINK_SECT:=$(_MTB_RECIPE__JLINKSCRIPT_SECT256)
ifeq (512,$(_MTB_RECIPE__DEVICE_FLASH_KB))
_MTB_RECIPE__FLASH_VAR:=CY8C6xx6
else #(512,$(_MTB_RECIPE__DEVICE_FLASH_KB))
_MTB_RECIPE__FLASH_VAR:=CY8C6xx7
endif #(512,$(_MTB_RECIPE__DEVICE_FLASH_KB))

ifneq (,$(_MTB_RECIPE__IS_SECURE_DEVICE))
_MTB_RECIPE__FLASH_VAR:=CYB06xx7
_MTB_RECIPE__JLINK_SECT:=
_MTB_RECIPE__OPENOCD_DEVICE_CFG=psoc6_secure.cfg
endif

else ifeq (PSoC6A2M,$(_MTB_RECIPE__DEVICE_DIE))
_MTB_RECIPE__OPENOCD_DEVICE_CFG=psoc6_2m.cfg
_MTB_RECIPE__JLINK_SECT:=$(_MTB_RECIPE__JLINKSCRIPT_SECT256)
ifeq (1024,$(_MTB_RECIPE__DEVICE_FLASH_KB))
_MTB_RECIPE__FLASH_VAR:=CY8C6xx8
else #(1024,$(_MTB_RECIPE__DEVICE_FLASH_KB))
_MTB_RECIPE__FLASH_VAR:=CY8C6xxA
endif #(1024,$(_MTB_RECIPE__DEVICE_FLASH_KB))
ifneq (,$(_MTB_RECIPE__IS_SECURE_DEVICE))
_MTB_RECIPE__FLASH_VAR:=CYB06xxA
_MTB_RECIPE__JLINK_SECT:=
_MTB_RECIPE__OPENOCD_DEVICE_CFG=psoc6_2m_secure.cfg
endif

else ifeq (PSoC6A512K,$(_MTB_RECIPE__DEVICE_DIE))
_MTB_RECIPE__FLASH_VAR:=CY8C6xx5
_MTB_RECIPE__JLINK_SECT:=$(_MTB_RECIPE__JLINKSCRIPT_SECT256)
_MTB_RECIPE__OPENOCD_DEVICE_CFG=psoc6_512k.cfg
ifneq (,$(_MTB_RECIPE__IS_SECURE_DEVICE))
_MTB_RECIPE__FLASH_VAR:=CYB06xx5
_MTB_RECIPE__JLINK_SECT:=
_MTB_RECIPE__OPENOCD_DEVICE_CFG=psoc6_512k_secure.cfg
endif

else ifeq (FX3G2,$(_MTB_RECIPE__DEVICE_DIE))
_MTB_RECIPE__OPENOCD_DEVICE_CFG=fx3g2.cfg
_MTB_RECIPE__FLASH_VAR:=CYUSB401x
_MTB_RECIPE__JLINK_SECT:=$(_MTB_RECIPE__JLINKSCRIPT_SECT256)

else ifeq (PSoC6A256K,$(_MTB_RECIPE__DEVICE_DIE))
_MTB_RECIPE__FLASH_VAR:=CY8C6xx4
_MTB_RECIPE__JLINK_SECT:=$(_MTB_RECIPE__JLINKSCRIPT_SECT128)
ifneq (,$(findstring CY8C45,$(DEVICE)))
_MTB_RECIPE__OPENOCD_CHIP_NAME:=psoc4500
_MTB_RECIPE__OPENOCD_DEVICE_CFG=psoc4500.cfg
else
_MTB_RECIPE__OPENOCD_DEVICE_CFG=psoc6_256k.cfg
endif

else ifeq (TVIIBE1M,$(_MTB_RECIPE__DEVICE_DIE))
_MTB_RECIPE__OPENOCD_DEVICE_CFG=traveo2_1m_a0.cfg
_MTB_RECIPE__OPENOCD_CHIP_NAME:=traveo2
else ifeq (TVIIBE2M,$(_MTB_RECIPE__DEVICE_DIE))
_MTB_RECIPE__OPENOCD_DEVICE_CFG=traveo2_2m.cfg
_MTB_RECIPE__OPENOCD_CHIP_NAME:=traveo2
else ifeq (TVIIBE4M,$(_MTB_RECIPE__DEVICE_DIE))
_MTB_RECIPE__OPENOCD_DEVICE_CFG=traveo2_be_4m.cfg
_MTB_RECIPE__OPENOCD_CHIP_NAME:=traveo2_be_4m

else
$(call mtb__error,Incorrect part number $(DEVICE). Check DEVICE variable.)
endif

_MTB_RECIPE__JLINK_DEVICE_CFG_PROGRAM:=$(_MTB_RECIPE__FLASH_VAR)$(_MTB_RECIPE__JLINKSCRIPT_PROGRAM_CORE)$(_MTB_RECIPE__JLINK_SECT)$(_MTB_RECIPE__JLINKSCRIPT_TM_POST_FIX)
_MTB_RECIPE__JLINK_DEVICE_CFG_ATTACH:=$(_MTB_RECIPE__FLASH_VAR)$(_MTB_RECIPE__JLINKSCRIPT_ATTACH_CORE)
_MTB_RECIPE__JLINK_DEVICE_CFG_DEBUG:=$(_MTB_RECIPE__JLINK_DEVICE_CFG_ATTACH)$(_MTB_RECIPE__JLINK_SECT)$(_MTB_RECIPE__JLINK_DEBUG_SUFFIX)
# Architecture specifics

ifneq ($(OTA_SUPPORT),)
# OTA post-build script needs python.
CY_PYTHON_REQUIREMENT=true
endif
